# -*- coding: utf-8 -*-
from openerp.osv import osv
from openerp.osv import fields


class weixin_advertising_wall(osv.osv):
    _name = 'weixin.advertising.wall'

    def get_image_and_url(self, cr, uid, ids=None, context=None):
        if not ids:
            ids = self.search(cr, uid, [('enabled', '=', True), ('category_id', '=', False)], context=context)

        res = []
        for wall in self.browse(cr, uid, ids, context=context):
            val = {
                'name': wall.name,
                'image': '/website/image/weixin.advertising.wall/' + str(wall.id) + '/image',
            }

            if wall.type == 'product':
                val['url'] = '/mshop/product/?product_id=' + str(wall.product_id.id)
            elif wall.type == 'url':
                val['url'] = wall.url
            elif wall.type == 'document':
                val['url'] = '/mshop/document?wall_id=' + str(wall.id)  # 文档的链接

            res.append(val)

        return res

    _columns = {
        'name': fields.char(u'名称'),
        'category_id': fields.many2one('mshop.article.category', u'栏目'),
        'image': fields.binary('图片'),
        'product_id': fields.many2one('product.template', u'产品'),
        'enabled': fields.boolean(u'可用'),
        'document': fields.html(u'文档内容'),
        'url': fields.char(u'链接'),
        'type': fields.selection([('product', '产品'), ('document', '文档'), ('url', u'链接')], u'类型'),
    }

    _defaults = {
        'enabled': True,
        'type': 'product',
    }


class weinxin_homepage_categories(osv.osv):
    _name = 'weinxin.homepage.categories'
    _order = 'index'

    def get_image_and_url(self, cr, uid, ids=None, context=None):
        if not ids:
            ids = self.search(cr, uid, [('enabled', '=', True)], context=context)

        res = []
        for category in self.browse(cr, uid, ids, context=context):
            val = {'mode': category.display_type}
            index = 1
            for index, detail in enumerate(category.detail_ids, 1):
                small_vals = detail.get_image_and_url(context=context)[0]
                val['small_' + str(index)] = {'img': small_vals.get('image'), 'url': small_vals.get('url')}

            res.append(val)

        return res

    def check_category_details_ids(self, cr, uid, ids, context=None):
        # 校验不同类型的栏目展示限制的产品显示数量
        for categories in self.browse(cr, uid, ids, context=context):
            if categories.display_type == 'single' and len(categories.detail_ids) == 1:
                return True
            if categories.display_type == 'horizontal' and len(categories.detail_ids) == 2:
                return True
            if categories.display_type == 'mix' and len(categories.detail_ids) == 3:
                return True

            return False

    _columns = {
        'name': fields.char(u'名称'),
        # 'product_category_id': fields.many2one('product.public.category', u'商城分类', required='True'),
        'enabled': fields.boolean(u'是否可用'),
        'display_type': fields.selection([('single', u'单图'), ('horizontal', u'水平均分'), ('mix', u'混合三分')], u'显示方式'),
        'index': fields.integer(u'顺序'),
        'detail_ids': fields.one2many('weixin.advertising.wall', 'category_id', string='配置详情'),
    }

    _defaults = {
        'enabled': True,
    }

    _constraints = [(check_category_details_ids, u'配置详情与显示方式不匹配', ['detail_ids', 'display_type'])]
